package yyds.sniarbtej.transformer.dbeaver;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodNode;
import yyds.sniarbtej.transformer.BaseClassTransformer;
import yyds.sniarbtej.transformer.ClassRewriter;
import yyds.sniarbtej.utils.DebugUtil;

public class PublicServiceClient extends BaseClassTransformer {
    public String canTransformClassName() {
        return "com/dbeaver/model/license/validate/PublicServiceClient";
    }

    public byte[] rewrite(byte[] in) {
        byte[] out = transform(in, new ClassRewriter() {
            @Override
            public void rewriteMethod(MethodNode node) {
                if ("ping".equals(node.name) && "()Ljava/lang/String;".equals(node.desc)) {
                    InsnList insn = new InsnList();
                    insn.add(new LdcInsnNode("pong"));
                    insn.add(new InsnNode(Opcodes.ARETURN));
                    node.instructions.insert(insn);
                } else if ("checkCustomerEmail".equals(node.name) && "(Lcom/dbeaver/lm/api/LMProduct;Ljava/lang/String;)Ljava/lang/String;".equals(node.desc)) {
                    InsnList insn = new InsnList();
                    insn.add(new LdcInsnNode(""));
                    insn.add(new InsnNode(Opcodes.ARETURN));
                    node.instructions.insert(insn);
                } else if ("checkLicenseStatus".equals(node.name) && "(Lcom/dbeaver/lm/api/LMLicense;Lcom/dbeaver/lm/api/LMProduct;)Ljava/lang/String;".equals(node.desc)) {
                    InsnList insn = new InsnList();
                    insn.add(new LdcInsnNode("VALID: Ok"));
                    insn.add(new InsnNode(Opcodes.ARETURN));
                    node.instructions.insert(insn);
                }
            }
        });
        DebugUtil.dump(canTransformClassName(), out);
        return out;
    }
}
